﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using RRRSRoguelike.Managers;
using RRRSRoguelike.Entities;
using RRRSRoguelike.Enums;

namespace RRRSRoguelike.Helpers.Actions
{
    public static class ActionHelper
    {
        public static void Get(Point position)
        {
        	      	
        	if (DungeonManager.Instance.PropManager.Props.OfType<Weapon>().
        	    Where(p => p.Position == position).Count() > 0 &&
                DungeonManager.Instance.CreatureManager.Player.WeaponSlot == null)
            {
                Weapon weapon = DungeonManager.Instance.PropManager.GetBestWeapon(position);
                DungeonManager.Instance.CreatureManager.Player.AddWeapon(weapon);
                DungeonManager.Instance.PropManager.Remove(weapon);
            }
        }

        public static void Put(Point point)
        {
            if (DungeonManager.Instance.CreatureManager.Player.WeaponSlot != null)
            {
                Weapon w = DungeonManager.Instance.CreatureManager.Player.WeaponSlot;
                w.Position = DungeonManager.Instance.CreatureManager.Player.Position;
                DungeonManager.Instance.CreatureManager.Player.RemoveWeapon();
                DungeonManager.Instance.PropManager.Add(w);
            }
        }
        
        public static bool Remove()
        {
        	//shortens calls.
            ActionManager ActionManager = DungeonManager.Instance.ActionManager;
       		Player player = DungeonManager.Instance.CreatureManager.Player;
            
            
       		bool built = false;
       		bool build = true;
       			
       		while(build)
       		{
       			Direction dir = DungeonManager.Instance.DungeonScreen.GetDirectionFromUser();
       			
       			Point newPosition = DungeonLevel.GetNewPosition(player.Position,dir,1);
       				
       			Tile t = (Tile)DungeonManager.Instance.DungeonLevel.GetTile(newPosition,Layers.Prop);
       			
       			if (t != null)
       			{
       				DungeonManager.Instance.PropManager.Remove(t);
   					built = true;
 			    	build = false;
   					DungeonManager.Instance.ForceRedraw();
       			}  	
       			    
       			else if (ActionManager.IsActionPushed(Enums.Actions.Quit) ||
                         ActionManager.IsActionPushed(Enums.Actions.Build))
                {
       			   	build = false;
					built = false;       			    	
                }       			
       			    	    
       			}
       		return built;
       		
        }
        
        public static bool Build(Tile tile)
		{
       		//shortens calls.
            ActionManager ActionManager = DungeonManager.Instance.ActionManager;
       		Player player = DungeonManager.Instance.CreatureManager.Player;
            
            
       		bool built = false;
       		bool build = true;
       			
       		while(build)
       		{
       			Direction dir = DungeonManager.Instance.DungeonScreen.GetDirectionFromUser();
       			
       			Point newPosition = DungeonLevel.GetNewPosition(player.Position,dir,1);
       				
       			if (DungeonManager.Instance.DungeonLevel.IsOKToPlaceMonster(newPosition))
       			    {
       					tile.Position = newPosition;
       			    	DungeonManager.Instance.PropManager.Add(tile);
       			    	DungeonManager.Instance.ForceRedraw();
       			    	built = true;
       			    	build = false;
       			    	
       			    }
       				else if (ActionManager.IsActionPushed(Enums.Actions.Quit) ||
                         ActionManager.IsActionPushed(Enums.Actions.Build))
                	{
       			    	build = false;
						built = false;       			    	
                	}       			       			
       			
       		}
       		return built;

       		}

    }
}
